import request from './request.js'

// 需要登陆的，都写到这里，否则就是不需要登陆的接口
const methodsLogin = [
	'/addons/mental/test/start',
	'/addons/mental/testlog/skip_result',
	'/addons/mental/testlog/mark_result',
	'/addons/mental/testlog/test_result',
	'/addons/mental/testlog/liang_result',
	'/addons/mental/testlog/my_test',
	'/addons/mental/remark/add_remark',
	'/addons/mental/remark/my_list',
	'/addons/mental/user/myinfo',
	'/addons/mental/user/update_avatar',
	'/addons/mental/order/create',
	'/addons/mental/order/detail',
	'/addons/mental/order/pay',
	'/addons/mental/feedback/add',
	'/addons/mental/feedback/get_list',
	'/addons/mental/user/getopenid'
];

//设置session_id
const getSessionId = function(vm) {
	let session = vm.$common.getDb('session');	
	if (!session) {
		let guid = vm.$common.guid();		
		vm.$common.setDb('session', guid);
		return guid;
	}
	return session;
}

const install = (Vue, vm) => {
	let url = 'http://www.fa.com'
	// #ifdef H5
	typeof window.fastUrl !== 'undefined' ? url = window.fastUrl : '',
	// #endif
	request.setConfig({
		baseUrl: url,
		header: {
			'Content-Type': 'application/json', //自定义请求头信息
		}
	});

	//请求拦截，配置Token等参数
	// type:request,upload
	request.interceptor.request(function(type, config) {
		config.header.sid = getSessionId(vm);
		config.header.token = vm.vuex_user.token ? vm.vuex_user.token : '';
		if (type == 'request') {
		} else {

		}
		return config;
	})

	//响应拦截，判断状态码是否通过
	request.interceptor.response(function(type, res) {
		console.log(res)
		if (type == 'request') {
			
			return res;
		} else {
			let data = JSON.parse(res);
			return data;
		}
	}, function(type, err) {
		console.log('api-error',err)
		if (type == 'request') {			
			vm.$common.errorToShow(err.msg);
			if(err.code==401){
				vm.$vuex('vuex_user',{});
				// #ifdef H5 || APP-PLUS
				vm.$common.navigateTo('/pages/login/login')
				// #endif
				// #ifdef MP-WEIXIN || MP-TOUTIAO
				vm.$common.navigateTo('/pages/login/choose')
				// #endif
			}
		} else {

		}
		return err;
	})
	//获取基础设置
	vm.$api.getSetting = () => request.get('/addons/mental/common/config');
	//上传文件
	vm.$api.upload = (data={}) => request.upload(vm.vuex_config.upload.uploadurl,data)
	//小程序登录1
	vm.$api.login1 = (data = {}) => request.get('/addons/mental/wx_user/login1', data);
	//小程序登录2
	vm.$api.login2 = (data = {}) => request.post('/addons/mental/wx_user/login2', data);
	//头条登录
	vm.$api.ttlogin1 = (data = {}) => request.get('/addons/mental/tt_user/login1', data);
	//头条登录
	vm.$api.ttlogin2 = (data = {}) => request.post('/addons/mental/tt_user/login2', data);
	//发送短信验证码
	vm.$api.smsSend = (data = {}) => request.post('/addons/mental/sms/send', data);
	//验证码登录
	vm.$api.mobilelogin = (data = {}) => request.post('/addons/mental/user/mobilelogin', data);
	//公众号获取openid
	vm.$api.getopenid = (data = {}) => request.post('/addons/mental/user/getopenid', data);
	//热门测试
	vm.$api.hottest = (data = {}) => request.get('/addons/mental/test/hottest', data);
	//测试列表
	vm.$api.testlist = (data = {}) => request.get('/addons/mental/test/getlist', data);
	//关键词搜索标题列表
	vm.$api.seachtitle = (data = {}) => request.get('/addons/mental/test/seachtitle', data);
	//测试详情
	vm.$api.testdetail = (data = {}) => request.get('/addons/mental/test/detail', data);
	//分类列表
	vm.$api.category = (data = {}) => request.get('/addons/mental/category/category', data);
	//创建订单
	vm.$api.orderCreate = (data = {}) => request.post('/addons/mental/order/create', data);
	//开始测试
	vm.$api.startTest = (data = {}) => request.post('/addons/mental/test/start', data);
	//跳转类型的结果提交
	vm.$api.skip_result = (data = {}) => request.post('/addons/mental/testlog/skip_result', data);
	//打分类型的结果提交
	vm.$api.mark_result = (data = {}) => request.post('/addons/mental/testlog/mark_result', data);
	//量表类型的结果提交
	vm.$api.liang_result = (data = {}) => request.post('/addons/mental/testlog/liang_result', data);
	//测试结果
	vm.$api.test_result = (data = {}) => request.get('/addons/mental/testlog/test_result', data);
	//添加评论
	vm.$api.add_remark = (data = {}) => request.post('/addons/mental/remark/add_remark', data);
	//公共评论列表
	vm.$api.remark_list = (data = {}) => request.get('/addons/mental/remark/get_list', data);
	//我评论的列表
	vm.$api.my_remark = (data = {}) => request.get('/addons/mental/remark/my_list', data);
	//我的信息
	vm.$api.myinfo = (data = {}) => request.get('/addons/mental/user/myinfo', data);
	//修改信息
	vm.$api.update_info = (data = {}) => request.post('/addons/mental/user/update_info', data);
	//我的测试
	vm.$api.my_test = (data = {}) => request.get('/addons/mental/testlog/my_test', data);
	//订单信息
	vm.$api.order_detail = (data = {}) => request.get('/addons/mental/order/detail', data);
	//订单支付
	vm.$api.order_pay = (data = {}) => request.post('/addons/mental/order/pay', data);
	//订单支付
	vm.$api.tt_pay = (data = {}) => request.get('/addons/mental/order/tt_pay', data);
	//微信授权手机号码登录
	vm.$api.getWechatMobile = (data = {}) => request.post('/addons/mental/wx_user/getWechatMobile', data);
	//公众号跳转地址
	vm.$api.mpAuth = (data = {}) => request.post('/addons/mental/wx_user/mpAuth', data);
	//新用户公众号登录
	vm.$api.authLogin = (data = {}) => request.post('/addons/mental/user/authLogin', data);
	//意见反馈
	vm.$api.addfeedback = (data = {}) => request.get('/addons/mental/feedback/add', data);
	//我的反馈记录
	vm.$api.feedbacklist = (data = {}) => request.get('/addons/mental/feedback/get_list', data);

}
export default {
	install
}
